const { ipcMain } = require('electron');
require('events').EventEmitter.defaultMaxListeners = 0;
const {ShapefileLoader} = require('@loaders.gl/shapefile');
require('@loaders.gl/polyfills');
const {load} = require('@loaders.gl/core');
const db = require('./db');
const os = require('os');
const processes = {};
class MainIPC {
    constructor () {
        ipcMain.on("load-shp-file", async (e, filePath) => {
            let data = await load(filePath, ShapefileLoader);
            e.returnValue = data;
        });
        ipcMain.on("minimize_win", async (e, name) => {
            let process = processes[name];
            if (process) {
                process.minimize();
            }
        });
        ipcMain.on("hide_win", async (e, name) => {
            let process = processes[name];
            if (process) {
                if (os.platform() === 'win32') {
                    process.close();
                } else {
                    process.close();
                }
            }
        });
    }

    /**
     * 注册窗口
     * @param {*} name 
     * @param {*} process 
     */
    _register (name, process) {
        processes[name] = process;
    }

    /**
     * 发布
     * @param {*} eventName 
     * @param {*} params 
     */
    emit (eventName, params) {
        for (let name in processes) {
            let process = processes[name];
            process && process.webContents && process.webContents.send(eventName, params);
        }
    }
}

module.exports = new MainIPC();